package gui.panels;

import gui.tools.ActionHandler;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mysql.MusicQueries;
import objects.Music;

public class MusicPanel extends JPanel {

	public Music mucurrentEntry;
	public MusicQueries mumusicQueries;
	public List<Music> muResults;
	public int munumberOfEntries = 0;
	public int muCurrentEntryIndex = 0;

	public JTextField muidText;
	private JLabel muidLabel;
	public JTextField albumText;
	private JLabel albumLabel;
	public JTextField artistText;
	private JLabel artistLabel;
	public JTextField lidText;
	private JLabel lidLabel;
	public JTextField outText;
	private JLabel outLabel;

	public JButton next;
	public JButton previous;
	public JPanel navigatePanel;
	public JTextField index;
	private JPanel displayPanel;
	public JButton add;

	public MusicPanel(ActionHandler ac) {
		getData();
		muidLabel = new JLabel("Music ID:");
		muidText = new JTextField(2);
		albumLabel = new JLabel("Artist:");
		albumText = new JTextField(10);
		artistLabel = new JLabel("Album:");
		artistText = new JTextField(10);
		lidLabel = new JLabel("Location ID:");
		lidText = new JTextField(2);
		outLabel = new JLabel("Is out:");
		outText = new JTextField(5);
		
		add = new JButton("Add new music");

		if (muResults.get(muCurrentEntryIndex).isOut() == 0) {
			outText.setText("False");
		} else
			outText.setText("True");

		next = new JButton("Next");
		previous = new JButton("Previous");
		navigatePanel = new JPanel();
		index = new JTextField(2);
		displayPanel = new JPanel();

		muidText.setEditable(false);
		albumText.setEditable(false);
		artistText.setEditable(false);
		lidText.setEditable(false);
		outText.setEditable(false);

		setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

		next.addActionListener(ac);
		previous.addActionListener(ac);
		navigatePanel.setLayout(new BoxLayout(navigatePanel, BoxLayout.X_AXIS));
		navigatePanel.add(previous);
		navigatePanel.add(Box.createHorizontalStrut(10));
		navigatePanel.add(index);
		navigatePanel.add(Box.createHorizontalStrut(10));
		navigatePanel.add(next);
		add(navigatePanel);

		displayPanel.setLayout(new GridLayout(5, 2, 4, 4));
		displayPanel.add(muidLabel);
		muidText.setEditable(false);
		displayPanel.add(muidText);
		displayPanel.add(albumLabel);
		displayPanel.add(albumText);
		displayPanel.add(artistLabel);
		displayPanel.add(artistText);
		displayPanel.add(lidLabel);
		displayPanel.add(lidText);
		displayPanel.add(outLabel);
		displayPanel.add(outText);
		
		add.addActionListener(ac);

		add(displayPanel);
		add(add, BorderLayout.SOUTH);
	}

	public void getData() {
		mumusicQueries = new MusicQueries();
		muResults = mumusicQueries.getAllMusic();
	}
}
